package com.lisp.springframework.web;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.VersionResourceResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.lisp.springframework.web.controller", 
	includeFilters = @Filter(Controller.class))
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
    
    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**")
			.addResourceLocations("/pages/", "classpath:/static/")
			.setCachePeriod(31556926)
			.resourceChain(true)  // 这里缓存的是指向文件的Resource对象(缓存指针)，所以不会导致浏览器看不到最新的文件
			.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")); // 注意这里需要去掉外层的/resources
		// version测试URL: http://localhost:8080/springframework/resources/index-23a7cb96a6a5ee0f4db34325287c58ff.htm 
	}
	
	@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/resources/**")  // 静态资源默认允许跨域请求
            .allowedOrigins("*")
            .allowedMethods("GET", "HEAD", "OPTIONS")
            .allowedHeaders("lisp-name")
            .exposedHeaders("lisp-name")
            //.allowCredentials(true)
            .maxAge(3600);
        // Add more mappings...
    }
	
	@Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
	    registry.freeMarker();
	    registry.viewResolver(new InternalResourceViewResolver());
    }
	
	@Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("/WEB-INF/freemarker");
        return configurer;
    }
	
	@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
            .indentOutput(true)
            .dateFormat(new SimpleDateFormat("yyyy-MM-dd"));
	    converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }
}
